package Dome04;
import java.util.Arrays;
import java.util.Comparator;

public class Test {
    public static void bubbleSort(Comparable[] comparables) {
        for (int i = 0; i < comparables.length-1; i++) {
            for (int j = 0; j < comparables.length-i-1; j++) {
                if(comparables[j].compareTo(comparables[j+1])>0) {
                    Comparable tem = comparables[j];
                    comparables[j] = comparables[j+1];
                    comparables[j+1] = tem;
                }
            }
        }
    }
    public static void main(String[] args) {
        Person[] people = new Person[3];
        people[0] = new Person(111,"a");
        people[1] = new Person(101,"b");
        people[2] = new Person(121,"c");
        System.out.println(Arrays.toString(people));
        bubbleSort(people);
        System.out.println(Arrays.toString(people));

    }
}

class Person implements Comparable<Person>{
    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }

    int age;
    String name;
    Person(int age, String name) {
        this.age = age;
        this.name = name;
    }

    @Override
    public int compareTo(Person o) {
        return this.age-o.age;
    }
}
//        return this.name.compareTo(o.name);

/*if(this.age>o.age) {
            return 1;
        } else if(this.age==o.age) {
            return 0;
        } else {
            return -1;
        }*/

/*

class NameCompare implements Comparator<Person> {
    @Override
    public int compare(Person o1, Person o2) {
        return o2.name.compareTo(o1.name);
    }
}
class AgeCompare implements Comparator<Person> {
    @Override
    public int compare(Person o1, Person o2) {
        return o2.age-o1.age;
    }
}*/
/*    public static void main01(String[] args) {
        Person[] people = new Person[3];
        people[0] = new Person(111,"a");
        people[1] = new Person(101,"b");
        people[2] = new Person(121,"c");
        System.out.println(Arrays.toString(people));
//        Arrays.sort(people);
        AgeCompare ageCompare = new AgeCompare();
        Arrays.sort(people,ageCompare);
        System.out.println("===========================");
        System.out.println(Arrays.toString(people));
    }*/